package day013.IBatis_Spring;

import java.util.List;

import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.test.jdbc.SimpleJdbcTestUtils;

public class DeptApp {
	public static void main(String[] args) {
		SetUpDB();
		
		ApplicationContext context = new FileSystemXmlApplicationContext("src/day013/IBatis_Spring/test.xml");
		
		DeptDao dao = (day013.IBatis_Spring.DeptDao)context.getBean("deptDao");
		
		DeptVO vo = dao.info(20);
		System.out.println("1 : " + vo.toString());
		
		DeptVO inVo = new DeptVO();
		inVo.setNo(60);
		inVo.setName("snoopy");
		inVo.setLocation("tistory");
		
		Object obj = dao.add(inVo);
		System.out.println("2 : " + obj);
		
		List<DeptVO> list = dao.list();
		System.out.println("3 : " + list.toString());
		
		int removeSuccess = dao.remove(50);
		System.out.println("4 : " + ((removeSuccess == 1) ? "삭제 성공": "삭제 실패"));
	}
	
	private static void SetUpDB() {
		BasicDataSource dataSource = new BasicDataSource();
		dataSource.setDriverClassName("net.sf.log4jdbc.DriverSpy");
		dataSource.setUrl("jdbc:log4jdbc:hsqldb:hsql://localhost/sampledb");
		dataSource.setUsername("sa");
		dataSource.setPassword("");
		dataSource.setDefaultAutoCommit(true);
		dataSource.setPoolPreparedStatements(true);
		        
        SimpleJdbcTestUtils.executeSqlScript(
        		new SimpleJdbcTemplate(dataSource), 
        		new ClassPathResource("day012/IBatis/sample_schema_hsql.sql"), 
        		true);		
	}
}
